import sqlite3
import os

def get_db_connection():
    """创建并返回数据库连接"""
    conn = sqlite3.connect('todos.db')
    conn.row_factory = sqlite3.Row  # 使返回的行像字典一样可访问
    return conn

def init_db():
    """初始化数据库表"""
    if not os.path.exists('todos.db'):
        conn = get_db_connection()
        with open('schema.sql', 'r') as f:
            conn.executescript(f.read())
        conn.commit()
        conn.close()
        print("数据库初始化完成")